package com.sophialex.health.record.common.showKtable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.nebula.widgets.cdatetime.CButton;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

import com.sophialex.health.record.common.Messages;
import com.sophialex.health.system.frame.dialog.DialogManager;
import com.sophialex.health.system.frame.util.Assert;
import com.sophialex.health.system.frame.util.LayoutUtil;
import com.sophialex.health.system.frame.widgets.dbTable.DbTable;

import de.kupzog.ktable.SWTX;

/**
 * showKtable属性设置;可以设置列的顺序,显示列
 * 
 * @author Administrator
 * 
 */
public class ShowKtableSettingDlg extends Dialog {

	protected Object result;
	protected Shell shell;
	private String[] cloumnTitle;
	private String[] columnPropertie;
	private int[] columnWidths;
	private int[] columnStyle;
	public final static int L = SWTX.ALIGN_HORIZONTAL_LEFT;
	private Composite cpt_bottom;
	private Table table;
	private Composite cpt_right;
	private CButton bt_up;
	private Button bt_exit;
	private CButton bt_down;
	private Button bt_ok;
	private DbTable dbTable;

	private String[] cloumnTitle_new;
	private String[] columnPropertie_new;
	private String[] curColumnPro;
	private int[] columnWidths_new;
	private int[] columnStyle_new;
	private ShowKTable ktable;
	private Button bt_qx;
	private Button bt_fx;

	/**
	 * Create the dialog.
	 * 
	 * @param parent
	 * @param style
	 * @param cloumnTitle
	 *            列名集合
	 * @param columnPropertie
	 *            列属性集合
	 * @param columnWidths
	 *            列宽度集合
	 * @param columnStyle
	 *            列样式集合
	 */
	public ShowKtableSettingDlg(Shell parent, ShowKTable ktable) {
		super(parent);
		this.ktable = ktable;
		this.cloumnTitle = ktable.getColumnTitles_all();
		this.columnPropertie = ktable.getColumnProperties_all();
		this.columnWidths = ktable.getColumnWidths_all();
		this.columnStyle = ktable.getColumnAligns_all();
		this.curColumnPro = ktable.getModel().getColumnProperties();
		setText(Messages.ShowKtableSettingDlg_0);
	}

	/**
	 * Open the dialog.
	 * 
	 * @return the result
	 */
	public Object open() {
		createContents();
		Display display = getParent().getDisplay();
		LayoutUtil.centerShell(display, shell);
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		shell.setLayout(new FormLayout());
		shell.setSize(350, 400);
		shell.setText(getText());

		{
			cpt_bottom = new Composite(shell, SWT.NONE);
			cpt_bottom.setLayout(new FormLayout());
			{
				FormData formData = new FormData();
				formData.height = 40;
				formData.right = new FormAttachment(100);
				formData.bottom = new FormAttachment(100);
				formData.left = new FormAttachment(0);
				cpt_bottom.setLayoutData(formData);
				{
					bt_exit = new Button(cpt_bottom, SWT.NONE);
					bt_exit.addSelectionListener(new SelectionAdapter() {
						public void widgetSelected(SelectionEvent e) {
							shell.close();
						}
					});
					{
						FormData formData_1 = new FormData();
						formData_1.bottom = new FormAttachment(100, -8);
						formData_1.right = new FormAttachment(100, -10);
						bt_exit.setLayoutData(formData_1);
					}
					bt_exit.setText(Messages.ShowKtableSettingDlg_1);
					bt_ok = new Button(cpt_bottom, SWT.NONE);
					bt_ok.addSelectionListener(new SelectionAdapter() {
						public void widgetSelected(SelectionEvent e) {
							okPress();
						}
					});
					{
						FormData formData_1 = new FormData();
						formData_1.bottom = new FormAttachment(bt_exit, 0,
								SWT.BOTTOM);
						formData_1.right = new FormAttachment(bt_exit, -19);
						bt_ok.setLayoutData(formData_1);
					}
					bt_ok.setText(Messages.ShowKtableSettingDlg_2);

					bt_qx = new Button(cpt_bottom, SWT.NONE);
					bt_qx.addSelectionListener(new SelectionAdapter() {
						public void widgetSelected(SelectionEvent e) {
							doSelectAll();
						}
					});
					{
						FormData formData_1 = new FormData();
						formData_1.bottom = new FormAttachment(100, -8);
						formData_1.left = new FormAttachment(6);
						bt_qx.setLayoutData(formData_1);
					}
					bt_qx.setText(Messages.ShowKtableSettingDlg_3);

					bt_fx = new Button(cpt_bottom, SWT.NONE);
					bt_fx.addSelectionListener(new SelectionAdapter() {
						public void widgetSelected(SelectionEvent e) {
							doFx();
						}
					});
					{
						FormData formData_1 = new FormData();
						formData_1.bottom = new FormAttachment(100, -8);
						formData_1.left = new FormAttachment(bt_qx, 6);
						bt_fx.setLayoutData(formData_1);
					}
					bt_fx.setText(Messages.ShowKtableSettingDlg_4);

				}
			}
		}
		{
			cpt_right = new Composite(shell, SWT.NONE);
			cpt_right.setLayout(new FormLayout());
			{
				FormData formData = new FormData();
				formData.width = 50;
				formData.top = new FormAttachment(0);
				formData.right = new FormAttachment(100);
				formData.bottom = new FormAttachment(cpt_bottom);
				cpt_right.setLayoutData(formData);
			}
			{
				bt_up = new CButton(cpt_right, SWT.NONE);
				bt_up.addSelectionListener(new SelectionAdapter() {
					public void widgetSelected(SelectionEvent e) {
						doUp();
					}
				});
				{
					FormData formData_1 = new FormData();
					formData_1.width = 25;
					formData_1.height = 25;
					formData_1.top = new FormAttachment(0, 120);
					formData_1.left = new FormAttachment(0, 10);
					bt_up.setLayoutData(formData_1);
				}
				bt_up.setText(Messages.ShowKtableSettingDlg_5);
				bt_down = new CButton(cpt_right, SWT.NONE);
				bt_down.addSelectionListener(new SelectionAdapter() {
					public void widgetSelected(SelectionEvent e) {
						doDown();
					}
				});
				{
					FormData formData_1 = new FormData();
					formData_1.height = 25;
					formData_1.width = 25;
					formData_1.top = new FormAttachment(bt_up, 40);
					formData_1.left = new FormAttachment(0, 10);
					bt_down.setLayoutData(formData_1);
				}
				bt_down.setText(Messages.ShowKtableSettingDlg_6);
			}
		}
		{
			dbTable = new DbTable(shell, SWT.BORDER | SWT.FULL_SELECTION
					| SWT.CHECK);
			table = dbTable.getTable();
			{
				FormData formData = new FormData();
				formData.right = new FormAttachment(cpt_right);
				formData.top = new FormAttachment(0);
				formData.left = new FormAttachment(0);
				formData.bottom = new FormAttachment(cpt_bottom);
				table.setLayoutData(formData);
			}
			table.setHeaderVisible(true);
			table.setLinesVisible(true);
		}

		createTable();
		setInput(createBean());
		initSelect();
	}

	private void initSelect() {
		for (TableItem item : table.getItems()) {
			ShowKTableBean bean = (ShowKTableBean) item.getData();
			item.setChecked(bean.isCheck());
		}
	}

	protected void doFx() {
		for (TableItem item : table.getItems()) {
			if (item.getChecked()) {
				item.setChecked(false);
			} else {
				item.setChecked(true);
			}
		}

	}

	protected void doSelectAll() {
		for (TableItem item : table.getItems()) {
			item.setChecked(true);
		}
	}

	protected void okPress() {
		try {
			Object[] objs = dbTable.getCheckedElements();
			int count = dbTable.getCheckedElements().length;
			ShowKTableBean bean;
			cloumnTitle_new = new String[count];
			columnPropertie_new = new String[count];
			columnWidths_new = new int[count];
			columnStyle_new = new int[count];
			for (int i = 0; i < count; i++) {
				bean = (ShowKTableBean) objs[i];
				if (bean != null) {
					cloumnTitle_new[i] = bean.getTitle();
					columnPropertie_new[i] = bean.getPropertie();
					columnWidths_new[i] = Integer.parseInt(bean.getWidth());
					columnStyle_new[i] = Integer.parseInt(bean.getStyle());
				}
			}
			ktable.setTitlesAndProperties(cloumnTitle_new, columnPropertie_new);
			ktable.setColumnAligns(columnStyle_new, false);
			ktable.setColumnWidths(columnWidths_new, false);
			ktable.saveStore();
			ktable.redraw();
			shell.close();
		} catch (Exception e) {
			DialogManager.invokeWarnDlg(e.getMessage() + Messages.ShowKtableSettingDlg_7);
		}

	}

	protected void doDown() {
		if (table.getSelectionCount() > 0) {
			dbTable.doDown();
		}
	}

	protected void doUp() {
		if (table.getSelectionCount() > 0) {
			dbTable.doUp();
		}
	}

	private void setInput(List<ShowKTableBean> list) {
		dbTable.setInput(list);
	}

	private void createTable() {
		{
			TableColumn column = new TableColumn(table, SWT.NONE);
			column.setText(Messages.ShowKtableSettingDlg_8);
			column.setWidth(45);
		}

		{
			TableColumn column = new TableColumn(table, SWT.NONE);
			column.setText(Messages.ShowKtableSettingDlg_9);
			column.setWidth(100);
		}
		dbTable.setColumnProperties(new String[] { "", "title" }); //$NON-NLS-1$ //$NON-NLS-2$
		dbTable.setRowheight(22);
		dbTable.setAutoResizeColumn(true);
		dbTable.setEntry(ShowKTableBean.class);
		dbTable.exeDbTable(ShowKTableBean.class);
	}

	private List<ShowKTableBean> createBean() {
		int index = cloumnTitle.length;
		ShowKTableBean bean;
		List<ShowKTableBean> list = null;
		if (cloumnTitle != null)
			list = new ArrayList<ShowKTableBean>();
		for (int i = 0; i < index; i++) {
			bean = new ShowKTableBean();
			bean.setTitle(cloumnTitle[i]);
			if (columnPropertie.length - 1 < i)
				bean.setPropertie(""); //$NON-NLS-1$
			else
				bean.setPropertie(columnPropertie[i]);
			if (columnWidths == null || columnWidths.length - 1 < i)
				bean.setWidth("100"); //$NON-NLS-1$
			else
				bean.setWidth(columnWidths[i] + ""); //$NON-NLS-1$
			if (columnStyle ==null || columnStyle.length - 1 < i)
				bean.setStyle(ShowKtableSettingDlg.L + ""); //$NON-NLS-1$
			else
				bean.setStyle(columnStyle[i] + ""); //$NON-NLS-1$
			if (!Assert.isNull(bean.getPropertie())) {
				bean.setIdx(getIdx(bean.getPropertie()));
				bean.setCheck(getCheck(bean));
			}

			list.add(bean);
		}
		sortList(list);
		return list;
	}

	private void sortList(List<ShowKTableBean> list) {
		Comparator comp = new Comparator() {
			public int compare(Object o1, Object o2) {
				ShowKTableBean bean = (ShowKTableBean) o1;
				ShowKTableBean bean2 = (ShowKTableBean) o2;
				if (bean.getIdx() > bean2.getIdx())
					return 1;
				return 0;
			}
		};
		Collections.sort(list, comp);
	}

	private boolean getCheck(ShowKTableBean bean) {
		if (curColumnPro != null && curColumnPro.length > 0) {
			for (String str : curColumnPro) {
				if (str.equals(bean.getPropertie())) {
					return true;
				}
			}
		}
		return false;
	}

	private int getIdx(String propertie) {
		if (curColumnPro != null && curColumnPro.length > 0) {
			int i = 0;
			for (String str : curColumnPro) {
				if (str.equals(propertie)) {
					return i;
				}
				i++;
			}
			return curColumnPro.length;
		} else {
			return 0;
		}
	}
}
